
package controllers;

import entity.Description;
import entity.Lists;
import entity.Notes;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.DateSelectEvent;
import services.implementation.DescriptionsService;
import services.implementation.ForHelp;
import services.implementation.ListsService;
import services.implementation.NotesService;

/*
 * @author smile
 */
@ManagedBean(name = "noteController")
@SessionScoped
public class NotesController implements Serializable {
    
    @EJB
    private DescriptionsService descriptionsService;
    @EJB
    private ListsService listsService;
    @EJB
    private NotesService notesService;
    
    private int _select_notes_for_delete;
    private Integer _isComplete;
    private String _text;
    private Integer _id;
    private int _current_list_id = 0;
    private int _current_note_id = 0;
    private Integer _idDescription;
    private String _textDescription;
    private Date _expiresDate;

    public Date getExpiresDate() {
        return _expiresDate;
    }

    public void setExpiresDate(Date _expiresDate) {
        this._expiresDate = _expiresDate;
    }
    
    public void setTextDescription(String _textDescription) {
        this._textDescription = _textDescription;
    }
    
    public int getCurrent_list_id() {
        return _current_list_id;
    }

    public void setCurrent_list_id(int _current_list_id) {
        this._current_list_id = _current_list_id;
    }
    public int getCurrent_note_id() {
        return _current_note_id;
    }

    public void setCurrent_note_id(int _current_note_id) {
        this._current_note_id = _current_note_id;
    }
    
    public int getSelect_notes_for_delete() {
        return _select_notes_for_delete;
    }

    public void setSelect_notes_for_delete(int _select_notes_for_delete) {
        this._select_notes_for_delete = _select_notes_for_delete;
    }
    public Integer getId() {
        return _id;
    }

    public void setId(Integer _id) {
        this._id = _id;
    }

    public Integer getIsComplete() {
        return _isComplete;
    }

    public void setIsComplete(Integer _isComplete) {
        this._isComplete = _isComplete;
    }

    public String getText() {
        return _text;
    }

    public void setText(String _text) {
        this._text = _text;
    }

    public Integer getIdDescription() {
        return _idDescription;
    }

    public void setIdDescription(Integer _idDescription) {
        this._idDescription = _idDescription;
    }

    public String getTextDescription() {
        return _textDescription;
    }

    /** Creates a new instance of NotesController */
    public NotesController() {
    }
    
    /*
     * Add new note to database
     */
    public String addNote (){
        try{                      
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            Notes note = new Notes();
            note.setId(1);
            note.setText(this._text);
            note.setIsPriority(0);
            note.setIsComplete(0);
            note.setDateOfPublication(new Date());
            note.setIdList(list);
            notesService.addNote(note);     
            return "List";
        }catch(Exception e){
            ForHelp.addErrorMessage(
                ResourceBundle
                .getBundle("/Bundle")
                .getString("Notes.Add.Error"));
            return null;
        }
    }
    
    
    public void handleDateSelect(DateSelectEvent event) {  
        Date date = event.getDate();
        try{
            notesService.setExpiresDate(this._current_note_id, date);
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
        }
    } 

    /*
     * Checks that list is selected. Return 1 if list selected
     */
    public Integer isSelectedList (){
        // if list is not selected
        if (this._current_list_id == 0){
            return 0;
        }
        return 1;
    }
    
    /*
     * Checks that note is exsist. Return 1 if note(s) exist(s).
     */
    public Integer isNotesExist(){
        try{
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            if (notesService.getNotesByListId(list).isEmpty()){
                return 0;
            }
            return 1;
        }catch(Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return 0;
        }
    }
    
    /*
     * Checks if live note(s) is exsist. Return 1 if note(s) exist(s).
     * Return 1 if live note(s) is exsist. 
     */
    public Integer isNotesLiveExist(){
        try{
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            if (notesService.getNotesNotComplitedByList(list).isEmpty()){
                return 0;
            }
            return 1;
        }catch(Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return 0;
        }
    }
    
    /*
     * Get notes by current list.
     */
    public List<Notes> getNotesNoCompliteByList (){    
        try{
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            return notesService.getNotesNotComplitedByList(list);
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
        
    }
    
    /*
     * Update current note. Mark note as completed
     */
    public String setCompleteTrue (){    
        try{
            notesService.setComplete(this._current_note_id, 1);
            return "List";
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }  
    }
    
    /*
     * Update current note. Mark note as not completed
     */
    public String setCompleteFalse (){    
        try{
            notesService.setComplete(this._current_note_id, 0);
            return "List";
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }  
    }
    
    /*
     * Update current note. Mark note as priority
     */
    public String setPriorityTrue (){    
        try{
            notesService.setPriority(this._current_note_id, 1);
            return "List";
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
        
    }
    
    /*
     * Update current note. Mark note as not priority
     */
    public String setPriorityFalse (){    
        try{
            notesService.setPriority(this._current_note_id, 0);
            return "List";
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    } 

    /*
     * Delete note from database.
     */
    public String destroy() {
        performDestroy();
        return "List";
    }
    
    /*
     * Delete note from database.
     */
    private void performDestroy() {
        try {
            Notes note = new Notes();
            note = notesService.getNoteById(this._select_notes_for_delete);
            notesService.deleteNote(note);
//            ForHelp.addSuccessMessage(
//                    ResourceBundle
//                    .getBundle("/Bundle")
//                    .getString("Notes.Deleted")
//            );
        } catch (Exception e) {
            ForHelp.addErrorMessage(
                    e, 
                    ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Persistence.Error.Occured")
            );
        }
    }
    
    /*
     * Update Descriprion by current note.
     */
    public String editNoteDescription() {
        try {
            Notes note = new Notes();
            note = notesService.getNoteById(this._current_note_id);
            this._expiresDate = note.getDateExpires();
            // Get Description by current note
            List<Description> listDescription = 
                    descriptionsService.getDescriptionByNote(note);
            if (listDescription.isEmpty()){
                this._idDescription = 0;
                this._textDescription = "";
            }else {
                this._idDescription = listDescription.get(0).getId();
                this._textDescription = listDescription.get(0).getDescription();
            } 
            return "Edit_description";
        } catch (Exception e) {
            System.out.println(e.getMessage());
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    }
    
    /*
     * Checks if completed note is exist.
     */
    public Integer isNotesCompletedExist(){
        try{
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            if (notesService.getNotesComplitedByList(list).isEmpty()){
                return 0;
            }
        return 1;
        }catch(Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return 0;
        }
    }
    
    /*
     * Get completed notes by current list.
     */
    public List<Notes> getNotesCompliteByList (){    
        try{
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            return notesService.getNotesComplitedByList(list);
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
        
    }

    /*
     * Update description for note in database.
     */
    public String saveDescription() {
        try {
            Notes note = new Notes();
            note = notesService.getNoteById(this._current_note_id);
            Description des = new Description ();
            // if description for current notes does not exist
            if (this._idDescription.equals(0)){
                des.setId(1);
                des.setDescription(this._textDescription);
                des.setIdNotes(note);
                descriptionsService.addDescription(des);
            }else {
                des = descriptionsService.getDescriptionById(this._idDescription);
                des.setDescription(this._textDescription);
                descriptionsService.updateDescription(des);
            }
            return "List";
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    }   
}